//
//  ZYMainViewController.swift
//  WeiBo
//
//  Created by zhangyu on 16/9/19.
//  Copyright © 2016年 zhangyu. All rights reserved.
//

import UIKit
import SVProgressHUD
class ZYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //使用kvc方式给oc中的类设置只读属性的值
        let zyTabbar = ZYTabBar()
        
//        zyTabbar.composeButtonClosure = { [weak self] in
//            print("闭包调用")
//        ｝
        
        
        //设置代理对象
        zyTabbar.zyDelegate = self
        setValue(zyTabbar, forKey: "tabBar")
//        self.tabBar = ZYTabBar()
        
        zyTabbar.composeButtonClosure = {  [weak self] in
            print("闭包调用")
            if !ZYUserAccountViewModel.sharedUserAccountViewModel.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录")
                return
            }
//            //已经登录执行下列代码
//            let composeView = ZYComposeView()
//            //设置最上层的window
//            let window = UIApplication.shared.windows.last!
//            window.addSubview(composeView)
            if let target = self {
                let composeView = ZYComposeView()
                composeView.show(target: target)
            }
        }
        
        //添加子视图控制器
        addChildViewController(childController: ZYHomeViewController(), title: "主页", imageName: "tabbar_home")
        addChildViewController(childController: ZYMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: ZYDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: ZYProfileViewController(), title: "我的", imageName: "tabbar_profile")
    }
    //设置tintColor->统一设置tabbar的选中颜色
//    月早执行越好，一般会Appdelegate里面
    
    //添加子视图控制器的方法重载
    func addChildViewController( childController: UIViewController , title:String,imageName:String) {
        childController.tabBarItem.image = UIImage(named: imageName)
        //渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //设置字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.boldSystemFont(ofSize: 14)], for: .normal)
        //设置标题文字
        childController.title = title
        //创建导航视图控制器
        let nav = ZYNavigationViewController(rootViewController: childController)
        //添加子视图控制器
        addChildViewController(nav)
    }


}
extension ZYMainViewController:ZYTabBarDelegate {
    func didSelectedComposeButton() {
        print("代理调用")
    }
}
